var checkout = {}
checkout.init = function () {
    checkout.chooseAddr();
    checkout.choosePayWay();
    checkout.calculateBill();
    checkout.addNewAddress();
}
checkout.chooseAddr = function () {
    //收货地址展开折叠
    $(document).on('click', '.js-show-more', function (event) {
        if ($('.js-add-list').hasClass('active')) {
            $('.js-add-list li:gt(0)').hide();
            $('.js-add-list').removeClass('active')
        } else {
            $('.js-add-list li:gt(0)').show();
            $('.js-add-list').addClass('active')
        }
    })
    //选择送货地址
    $(document).on('click', '.js-address-item', function (event) {
        var $this = $(event.currentTarget);
        $('.js-address-item.selected').removeClass('selected');
        $($this).addClass('selected');
        $('.js-address').html($($this).find('input').val());
    })
}
//选择支付方式
checkout.choosePayWay = function () {
    $(document).on('click', '.js-pay-way', function (event) {
        $('.js-pay-way.selected').removeClass('selected');
        $(event.currentTarget).addClass('selected');
    })
}
//修改商品数量
checkout.calculateBill = function () {
    //数量加减
    $(document).on('click', '.js-reduce', function (event) {
        var $this = $(event.currentTarget);
        var $num = $this.parent('.count').find('.js-number'), number = parseInt($num.val());
        if (number > 1) {
            number--;
            $num.val(number);
            calculateTotal();
        } else {
            alert2("数量不能再少了哦~");
        }
    })
    $(document).on('click', '.js-add', function (event) {
        var $this = $(event.currentTarget);
        var $num = $this.parent('.count').find('.js-number'), number = parseInt($num.val());
        if (number < 999) {
            number++;
            $num.val(number);
            calculateTotal();
        } else {
            alert2("数量不能再多了哦~");
        }
    })
    var calculateTotal = function () {
        var list = $('.js-bill-table tr');
        var totalAmount = 0,fare = parseInt($('.js-fare').html());
        $.each(list, function (index, item) {
            var number = $(item).find('.js-number').val();
            var price = $(item).find('.js-price').html();
            var total = parseInt(number) * parseFloat(price);
            totalAmount += total;
            $(item).find('.js-total').html(total.toFixed(2));
        })
        $('.js-bill-amount').html(totalAmount.toFixed(2));
        $('.js-pay-amount').html((totalAmount + fare).toFixed(2));
    }
}
checkout.addNewAddress = function () {
    var model;
    $(document).on('click','.js-newaddress',function () {
        model = $.fn.modal.showModal({
            width:800,
            height:400,
            title: '新增收货地址',
            content:$("#js-add-newaddresss-modal").val()
        })
    })
    $(document).on('click','.js-submit-newaddress',function () {
        model.close().remove();//提交数据 关闭窗口
    })
}

var list = {}
list.init = function () {
    $(document).on('click','.js-sl-more',function (event) {
        var target = $('.js-s-brand-list');
        target.toggleClass('expand');
    })
    $(document).on('click','.js-brand-item',function (event) {
        var $this = $(event.currentTarget);
        $this.toggleClass("selected");
        $(document).trigger("update-select-brands");
    })
    $(document).on("update-select-brands",list.updateSelectBrands);
}
list.updateSelectBrands = function () {
    var html = [];
   $.each($(".js-s-brand-list").find(".js-brand-item.selected"),function (idx,item) {
       html.push("<span>",$(item).data("brand-name"),"</span>")
   })
    $(".js-selected-brand-list").html(html.join(""))
}
$(document).ready(function () {
    list.init();
})